import scrapy
from scrapy_bz.items import ScrapyBzItem


class BzSpider(scrapy.Spider):
    name = "bz"
    allowed_domains = ["www.toopic.cn"]
    start_urls = ["https://www.toopic.cn/dnbz/?q=--81--.html"]

    base_url = "https://www.toopic.cn/dnbz/?q=--81--.html"
    page = 1

    def parse(self, response):
        # print("=================")
        a_list = response.xpath('//div[@class="bd"]/a/img')

        for a in a_list:
            src = a.xpath("./@data-original").extract_first()
            name = a.xpath("./@alt").extract_first()
            print(name, src)

            picture = ScrapyBzItem(src=src, name=name)
            yield picture

            # 多页爬取 每一页的请求几乎一样
        if self.page < 3:
            self.page = self.page + 1

            url = self.base_url + '&page=' + str(self.page)

            # 怎么去调用parse方法 这里就是get请求 相当于递归
            yield scrapy.Request(url=url, callback=self.parse)
